import json
import traceback
from json import JSONDecodeError
from flask import jsonify
from werkzeug.exceptions import (
    HTTPException
)

from infra import logging_handler

logger = logging_handler.get_logger()

def invalid_request_body(e: JSONDecodeError):
    resp = {
        'code': 400,
        'message': 'invalid json format',
        'data': e
    }
    return jsonify(resp)


def http_error(e: HTTPException):
    response = e.get_response()
    response.content_type = "application/json"
    response.data = json.dumps({
        "code": e.code,
        "message": e.name,
        "data": e.description,
    })

    return response


def internal_error(e: Exception):
    resp = {
        'code': 500,
        'message': 'internal server error',
        'data': traceback.format_exc()
    }


    return jsonify(resp)
